package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.pojo.dto.UserIndexTicketDetailsDTO;
import cn.icanci.ladybird.pojo.entity.*;
import cn.icanci.ladybird.service.*;
import cn.icanci.ladybird.service.user.UserIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: icanci
 * @date: Created in 2020/10/1 19:10
 * @classAction: zxx
 */
@SuppressWarnings("all")
@Service
public class UserIndexServiceImpl implements UserIndexService {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private TicketDetailsTicketAddressMiddleService ticketDetailsTicketAddressMiddleService;
    @Autowired
    private TicketAddressService ticketAddressService;
    @Autowired
    private TicketDetailsTicketPriceMiddleService ticketDetailsTicketPriceMiddleService;
    @Autowired
    private TicketPriceService ticketPriceService;
    @Autowired
    private TicketDetailsTicketTimeMiddleService ticketDetailsTicketTimeMiddleService;
    @Autowired
    private TicketTimeService ticketTimeService;

    /**
     * int offset,int limit
     *
     * @return
     */
    @Override
    public List<UserIndexTicketDetailsDTO> userIndexTicketDetailsDTOList() {
        List<TicketDetails> ticketDetailsList = ticketDetailsService.queryManyByWeight(1, 6);
        // 需要封装的集合
        ArrayList<UserIndexTicketDetailsDTO> userIndexTicketDetailsDTOS = new ArrayList<>();
        // 遍历查到的票
        for (TicketDetails details : ticketDetailsList) {
            // 创建DTO对象
            UserIndexTicketDetailsDTO userIndexTicketDetailsDTO = new UserIndexTicketDetailsDTO();
            // 拿到 id
            Long ticketDetailsId = details.getTicketDetailsId();
            // 设置DTO数据对象的id 商品名字 图片地址
            userIndexTicketDetailsDTO.setTicketDetailsId(ticketDetailsId);
            // 标题
            userIndexTicketDetailsDTO.setTicketDetailsInfo(details.getTicketDetailsInfo());
            // 图片的连接地址
            userIndexTicketDetailsDTO.setTicketDetailsTicketImgUrl(details.getTicketDetailsTicketImgUrl());
            // 场馆
            userIndexTicketDetailsDTO.setTicketAddressVenue(details.getTicketAddressVenue());
            // 查询的是地点
            // 地点

            // 下一个多表
            // ticketDetailsId
            List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService
                    .queryByTicketDetailsId(ticketDetailsId);
            // 遍历集合
            ArrayList<Double> prices = new ArrayList<>();
            for (TicketDetailsTicketPriceMiddle middle : priceMiddles) {
                Long ticketPriceMiddleId = middle.getTicketPriceMiddleId();
                TicketPrice ticketPrice = ticketPriceService.queryById(ticketPriceMiddleId);
                if (null!=ticketPrice) {
                    prices.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
                }
            }
            prices.sort((p1, p2) -> {
                return p1.compareTo(p2);
            });
            userIndexTicketDetailsDTO.setTicketPricePrice(prices.get(0));

            // 查询时间
            List<TicketDetailsTicketTimeMiddle> timeMiddles = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetailsId);
            // 遍历集合
            ArrayList<Date> dates = new ArrayList<>();
            for (TicketDetailsTicketTimeMiddle middle : timeMiddles) {
                Long timeMiddleId = middle.getTicketTimeMiddleId();
                TicketTime ticketTime = ticketTimeService.queryById(timeMiddleId);
                if (null!=ticketTime) {
                    dates.add(ticketTime.getTicketTime());
                }
            }
            dates.sort((d1, d2) -> {
                return d1.compareTo(d2);
            });
            userIndexTicketDetailsDTO.setTicketStartTime(dates.get(0));
            userIndexTicketDetailsDTO.setTicketEndTime(dates.get(dates.size() - 1));
            // 封装到结果对象集合
            userIndexTicketDetailsDTOS.add(userIndexTicketDetailsDTO);
        }
        return userIndexTicketDetailsDTOS;
    }

    @Override
    public UserIndexTicketDetailsDTO userIndexTicketDetailsDTOBigOne() {
        UserIndexTicketDetailsDTO userIndexTicketDetailsDTO = new UserIndexTicketDetailsDTO();
        TicketDetails ticketDetails = ticketDetailsService.queryOneByWeight();
        Long ticketDetailsId = ticketDetails.getTicketDetailsId();
        // 设置DTO数据对象的id 商品名字 图片地址
        userIndexTicketDetailsDTO.setTicketDetailsId(ticketDetailsId);
        // 标题
        userIndexTicketDetailsDTO.setTicketDetailsInfo(ticketDetails.getTicketDetailsInfo());
        // 图片的连接地址
        userIndexTicketDetailsDTO.setTicketDetailsTicketImgUrl(ticketDetails.getTicketDetailsTicketImgUrl());
        // 场馆
        userIndexTicketDetailsDTO.setTicketAddressVenue(ticketDetails.getTicketAddressVenue());

        List<TicketDetailsTicketPriceMiddle> priceMiddles = ticketDetailsTicketPriceMiddleService.queryByTicketDetailsId(ticketDetailsId);
        // 遍历集合
        ArrayList<Double> prices = new ArrayList<>();
        for (TicketDetailsTicketPriceMiddle middle : priceMiddles) {
            Long ticketPriceMiddleId = middle.getTicketPriceMiddleId();
            TicketPrice ticketPrice = ticketPriceService.queryById(ticketPriceMiddleId);
            if (null!=ticketPrice) {
                prices.add(Double.valueOf((Double) ticketPrice.getTicketPricePrice()));
            }
        }
        prices.sort((p1, p2) -> {
            return p1.compareTo(p2);
        });
        userIndexTicketDetailsDTO.setTicketPricePrice(prices.get(0));
        return userIndexTicketDetailsDTO;
    }
}
